/**
 * 字典数据
 * <pre>
 * </pre>
 *@author MuYi
 *@date 2023/3/15 21:38
 *@version 1.0
 **/
import {DictData, DictMeta, Dict} from "./DictDataClass";
import {getDicts} from '@/api/dict'
import {requestDict} from "./DictLoad";

const DEFAULT_DICT_META = {
    type: '',
    labelField: 'dictValue',
    valueField: 'dictKey',
    request(dictType: string) {
        return getDicts(dictType).then(res =>{
            return (res ) ? (res) : [{}]}
        )
    }
}
/**
 * 加载字典数据
 * <pre>
 *     dictsTypeName约定：
 *     1 枚举enum类型以Enum开头
 *     2 自定义类型以"_"开头，如"_boolean"
 *     3 其他为系统提供dictType
 * </pre>
 * @param dictsTypeName 字典名称集合。字符串数组，或以逗号分隔的字符串
 * @param config
 * @constructor
 */
export const  LoadDict = (dictsTypeName: string[] | string, config = DEFAULT_DICT_META): Dict => {
    let result = new Dict(null);
    let typeNames: string[] = getDictTypeNameArray(dictsTypeName);
    if (typeNames.length < 1) return result;
    for (let typeName of typeNames) {
        config.type = typeName;
        result.type[typeName] = [];
        result.label[typeName] = [];
        let dicts = requestDict(config, result)
    }
    return result;
}

const getDictTypeNameArray = (dicts: string[] | string): string[] => {
    if (!dicts) return [];
    let result: string[];
    if (dicts instanceof Array) {
        result = dicts;
    } else {
        result = dicts.toString().split(",")
    }
    for (let i = 0; i < result.length; i++) {
        result[i] = result[i].trim();
    }
    return result
}
